package com.gexiaobao.pigeon.ui.organizer.fragment

import android.os.Bundle
import android.view.inputmethod.EditorInfo
import androidx.core.widget.addTextChangedListener
import androidx.recyclerview.widget.LinearLayoutManager
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.base.BaseFragment
import com.gexiaobao.pigeon.app.ext.hideSoftKeyboard
import com.gexiaobao.pigeon.app.ext.init
import com.gexiaobao.pigeon.app.ext.initFooter
import com.gexiaobao.pigeon.app.ext.loadListData
import com.gexiaobao.pigeon.app.ext.loadServiceInit
import com.gexiaobao.pigeon.app.ext.showLoading
import com.gexiaobao.pigeon.app.util.RxToast
import com.gexiaobao.pigeon.app.util.Util
import com.gexiaobao.pigeon.app.weight.recycleview.SpaceItemDecoration
import com.gexiaobao.pigeon.databinding.FragmentMemberInfoBinding
import com.gexiaobao.pigeon.ui.adapter.MemberInfoAdapter
import com.gexiaobao.pigeon.viewmodel.MemberInfoViewModel
import com.kingja.loadsir.core.LoadService
import me.hgj.jetpackmvvm.ext.nav
import me.hgj.jetpackmvvm.ext.navigateAction
import me.hgj.jetpackmvvm.ext.util.setOnclickNoRepeat

/**
 * @Author      : hxw
 * @Date        : 2023/6/1 12:17
 * @Describe    : 组织方下会员列表
 */
class FragmentMemberInfo : BaseFragment<MemberInfoViewModel, FragmentMemberInfoBinding>() {

    private var shedNo = ""
    private val mAdapter: MemberInfoAdapter by lazy { MemberInfoAdapter(arrayListOf()) }
    private lateinit var loadsir: LoadService<Any>

    override fun initView(savedInstanceState: Bundle?) {
        mDatabind.viewmodel = mViewModel

        mDatabind.etSearch.setOnEditorActionListener { _, i, _ ->
            when (i) {
                EditorInfo.IME_ACTION_SEARCH ->
                    searchMemberList()

                else -> {}
            }
            false
        }

        mDatabind.etSearch.addTextChangedListener {
            shedNo = it.toString().trim()
            if (shedNo.isEmpty()) {
                loadsir.showLoading()
                mViewModel.getMemberList(true, shedNo, "")
            }
        }

        mDatabind.layoutList.swipeRecyclerView.init(LinearLayoutManager(context), mAdapter).let {
            it.addItemDecoration(SpaceItemDecoration(0, Util.dp2px(context, 5f)))
            it.initFooter {
                //触发加载更多时请求数据
                mViewModel.getMemberList(false, shedNo, "")
            }
        }

        mDatabind.layoutList.swipeRefreshLayout.init {
            loadsir.showLoading()
            mViewModel.getMemberList(true, shedNo, "")
        }

        //状态页配置
        loadsir = loadServiceInit(mDatabind.layoutList.swipeRefreshLayout) {
            //点击重试时触发的操作
            loadsir.showLoading()
            mViewModel.getMemberList(true, shedNo, "")
        }

        loadsir.showLoading()
        mViewModel.getMemberList(true, shedNo, "")


        mAdapter.run {
            setOnItemClickListener { _, _, position ->
                nav().navigateAction(R.id.action_member_info_to_pigeon_list, Bundle().apply {
                    putString("title", mAdapter.data[position].entryName)
                    putInt("userId", mAdapter.data[position].userId)
                    putString("entryName", mAdapter.data[position].entryName)
                })
            }
        }

        mDatabind.etSearch.setOnEditorActionListener { _, i, _ ->
            when (i) {
                EditorInfo.IME_ACTION_SEARCH -> {
                    hideSoftKeyboard(activity)
                    loadsir.showLoading()
                    mViewModel.getMemberList(true, mViewModel.etSearchContent.get(), "")
                }

                else -> {}
            }
            false
        }
    }

    private fun searchMemberList() {
        loadsir.showLoading()
        mViewModel.getMemberList(true, shedNo, "")
    }

    override fun onBindViewClick() {
        super.onBindViewClick()
        setOnclickNoRepeat(mDatabind.tvSearch, mDatabind.etSearch) {
            when (it) {
                mDatabind.tvSearch -> {
                    if (shedNo.isEmpty()) {
                        RxToast.showToast("请输入要查询的棚号")
                        return@setOnclickNoRepeat
                    }
                    hideSoftKeyboard(activity)
                    loadsir.showLoading()
                    mViewModel.getMemberList(true, mViewModel.etSearchContent.get(), "")
                }

                mDatabind.etSearch -> {
                    mDatabind.etSearch.isFocusable = true
                    mDatabind.etSearch.isFocusableInTouchMode = true
                }
            }
        }
    }

    override fun createObserver() {
        super.createObserver()
        /**  获取组织方下会员列表  */
        mViewModel.memberInfoResult.observe(this) {
            loadListData(it, mAdapter, loadsir, mDatabind.layoutList.swipeRecyclerView, mDatabind.layoutList.swipeRefreshLayout)
        }
    }

    companion object {
        fun newInstance(): FragmentMemberInfo {
            return FragmentMemberInfo()
        }
    }
}